﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel.Concrete;
using DomainModel.Entities;
using System.IO;

namespace WebUI.Controllers
{
    public class TrackController : Controller
    {
        SqlTrackRepository _trackRepository;
        public TrackController()
        {
            _trackRepository = new SqlTrackRepository();
        }
        #region Private Region

        private ActionResult HandleMyError(Exception ex)
        {
            var modelErr = new ErrorHandling(ex);
            return View("Error", modelErr);
        }

        public void SaveAudioFile(Track track, HttpPostedFileBase audioFile)
        {
            try
            {
                string domainPath = AppDomain.CurrentDomain.BaseDirectory + "/Content/Audio" + "/";
                track.TrackContentSize = audioFile.ContentLength;
                track.TrackMimeType = audioFile.ContentType;
                track.TrackFileName = audioFile.FileName;
                track.TrackSavingName = track.Album.ArtistID.ToString() + "_" + track.AlbumID.ToString() + "_" + track.TrackID.ToString() + Path.GetExtension(audioFile.FileName);

                string filePath = domainPath + track.TrackSavingName;
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                audioFile.SaveAs(filePath);
                _trackRepository.Update(track);
            }
            catch (Exception ex)
            {
                HandleMyError(ex);
            }

        }

        #endregion

        public ActionResult Index(int id)  // this will be album id
        {
            return View(_trackRepository.GetAll().Where(t => t.AlbumID == id));
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Create(int id)
        {
            ViewData["referringUrl"] = Request.UrlReferrer;
            Track track = new Track();
            track.LastUpdate = System.DateTime.Now;
            track.AlbumID = id;
            return View(track);
        }

        [HttpPost]
        public ActionResult Create(Track track, HttpPostedFileBase audioFile, string referringUrl)
        {
            try
            {
                _trackRepository.Insert(track);
                if (audioFile.ContentLength > 0)
                {
                    SaveAudioFile(track, audioFile);
                }
                if (string.IsNullOrEmpty(referringUrl))
                {
                    return RedirectToAction("Details", "Album", new { id = track.AlbumID });
                }
                else
                {
                    return Redirect(referringUrl);
                }
            }
            catch (Exception ex)
            {
                return HandleMyError(ex);
            }
        }

        public ActionResult Edit(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
